<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        #iphone {
            position: relative;
            width: 426px;
            height: 640px;
            margin: 10px auto;
            background: url(image/1.jpg) no-repeat;
        }
        #lock {
            position: absolute;
            left: 50%;
            bottom: 33px;
            width: 358px;
            height: 62px;
            margin-left: -179px;
        }
        #lock span {
            position: absolute;
            width: 93px;
            height: 62px;
            cursor: pointer;
            background: url(image/btn.jpg) no-repeat;
        }
    </style>
    <script type="text/javascript">
        window.onload = function() {
            var iPhone = document.getElementById("iphone");
            var oLock = document.getElementById("lock");
            var oBtn = oLock.getElementsByTagName("span")[0];
            var disX = 0;
            var maxL = oLock.clientWidth - oBtn.offsetWidth;
            var oBg = document.createElement("img");
            oBg.src = "image/2.jpg"; //预加载第二张背景

            oBtn.onmousedown = function(e) {
                var e = e ||　window.event;
                // 触点初始化
                disX = e.clientX - this.offsetLeft;

                document.onmousemove = function(e) {
                    var e = e || window.event;
                    var l = e.clientX - disX;
                    // 设置左右界限
                    l < 0 && (l=0);
                    l > maxL && (l=maxL);

                    oBtn.style.left = l + "px";

                    oBtn.offsetLeft == maxL && (iPhone.style.background = "url(" + oBg.src + ")", oLock.style.display = "none");

                    return false;
                };
                document.onmouseup = function() {
                    document.onmousemove = null;
                    document.onmouseup = null;
                    // ??????
                    oBtn.releaseCapture &&　oBtn.releaseCapture();

                    oBtn.offsetLeft > maxL / 2 ?
                        startMove(maxL, function() {
                            iPhone.style.background = "url(" + oBg.src + ")";
                            oLock.style.display = "none";
                        }) :
                        startMove(0);
                };

                this.setCapture && this.setCapture();
                return false;
            }

            function startMove(iTarget, onEnd) {
                clearInterval(oBtn.timer);
                oBtn.timer = setInterval(function() {
                    doMove(iTarget, onEnd)
                }, 30)
            }
            function doMove(iTarget, onEnd) {
                var iSpeed = (iTarget - oBtn.offsetLeft) * 0.2;
                iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
                iTarget == oBtn.offsetLeft ? (clearInterval(oBtn.timer), onEnd && onEnd()) : oBtn.style.left = iSpeed + oBtn.offsetLeft + "px"
            }
        };
    </script>
</head>
<body>
    <div id="iphone">
        <div id="lock"><span></span></div>
    </div>
</body>
</html>